package Thread;

public class ThreadDemo10 {
    public static void main(String[] args) {
        Object object1=new Object();
        Object object2=new Object();
        Thread thread1=new Thread(()->{
            synchronized (object1){

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (object2){
                    System.out.println("object1把两个资源都访问到了");
                }

            };

        });


        Thread thread2=new Thread(()->{
            synchronized (object1){

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (object2){
                    System.out.println("object2把两个资源都访问到了");
                }

            };

        });
        thread1.start();
        thread2.start();
        try {
            thread1.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        try {
            thread2.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

    }
}
